using UnityEngine;
using System.Collections;

public class DeathCounterControl : StateBehaviour<DeathCounterControl> 
{
    #region Attributes

    /* Counter Speeds */
    public float deathTime = 10.0f;
    public float speedFilling = 1.0f;
    public float speedEmptying = 1.0f;

    [HideInInspector]
    public float count = 0.0f;
    public float cutoffInitial = 0.083333f;
    public float cutoffDeath = 0.916666f;
    public float cutoffMin = 0.0f;
    public float cutoffSlice = 0.166666f;
    public float cutoffTotal = 0.0f;
    public float deathTimeRelativeToCutoff = 0.0f;

    private float epsilon = 0.0001f; // Necesario para aproximar los calculos con decimales
    [HideInInspector]
    public PlayerControl player;

    #endregion

    #region Methods

    private void FindPlayer()
    {
        GameObject playerGameObject = GameObject.FindGameObjectWithTag(TagConstants.TAG_PLAYER);
        if (playerGameObject)
            this.player = playerGameObject.GetComponent<PlayerControl>();
        else
            Debug.LogError(TextConstants.PLAYER_NOT_FOUND_ERROR);
    }

    public void ShowDeathCounter()
    {
        this.renderer.enabled = true;
        this.transform.FindChild("LifeBarBackground").renderer.enabled = true;
        this.transform.FindChild("LifeBarOuter").renderer.enabled = true;
    }

    public void HideDeathCounter()
    {
        this.renderer.enabled = false;
        this.transform.FindChild("LifeBarBackground").renderer.enabled = false;
        this.transform.FindChild("LifeBarOuter").renderer.enabled = false;
    }

    public void ChangeEnergy(float quantity, bool permanent)
    {
        this.State = DeathCounterEmpty.Instance;
        this.cutoffTotal += quantity;
        if (permanent)
            this.cutoffMin += quantity;

        this.renderer.material.SetFloat("_Cutoff", this.cutoffTotal);
    }

    #endregion

    #region Unity Methods

    void Start () 
    {
        this.FindPlayer();
        this.cutoffTotal = this.cutoffInitial;
        this.cutoffMin = this.cutoffInitial;
        this.State = DeathCounterIdle.Instance;
        this.updateCalledIn = StateBehaviourMethodsName.Update;
    }

    #endregion

    #region StateBehaviour

    public override void OnUpdate()
    {
        this.deathTimeRelativeToCutoff = this.deathTime * (1 - this.cutoffMin);

        if (this.cutoffTotal + this.epsilon >= this.cutoffDeath)
        {
            this.player.Die();
            this.updateCalledIn = StateBehaviourMethodsName.None;
        }

        if (this.State == DeathCounterIdle.Instance && this.player.enemiesInAlert > 0)
        {
            //this.ShowDeathCounter();
            this.State = DeathCounterEmpty.Instance;
        }

        if (this.State == DeathCounterIdle.Instance && this.player.enemiesInAlert <= 0)
        {
            //this.HideDeathCounter();
        }

        if (this.State == DeathCounterEmpty.Instance && this.player.enemiesInAlert <= 0)
        {
            this.State = DeathCounterFill.Instance;
        }

        if (this.State == DeathCounterFill.Instance && this.player.enemiesInAlert > 0)
        {
            this.State = DeathCounterEmpty.Instance;
        }
    }

    public override DeathCounterControl GetBehaviuor()
    {
        return this;
    }

    #endregion
}
